package com.citybox.shared.web.page;

import cn.hutool.core.util.ObjectUtil;
import com.citybox.shared.text.Convert;
import com.citybox.shared.utils.ServletUtils;
import com.citybox.shared.utils.StringUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页数据
 */
@Data
public class PageDomain {
    /**
     * 页码
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER_BY_COLUMN = "orderByColumn";

    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    public static final String IS_ASC = "isAsc";

    /**
     * 分页参数合理化
     */
    public static final String REASONABLE = "reasonable";

    /**
     * 页码
     */
    private Integer pageNum;

    /**
     * 每页、记录数
     */
    private Integer pageSize;

    private List<OrderBy> orderBys;

    /**
     * 分页参数合理化
     */
    private Boolean reasonable = true;

    public Boolean getReasonable() {
        if (StringUtils.isNull(reasonable)) {
            return Boolean.TRUE;
        }
        return reasonable;
    }

    public String getOrderBys() {
        String orderBysStr = "";
        if (ObjectUtil.isNull(orderBys)) {
            return orderBysStr;
        } else {
            if (orderBys.size() > 1) {
                for (OrderBy orderBy : orderBys) {
                    orderBysStr += orderBy.getOrderBy() + ",";
                }
                orderBysStr = orderBysStr.substring(0, orderBysStr.length() - 1);
            } else if (orderBys.size() == 1) {
                orderBysStr = orderBys.get(0).getOrderBy();
            }
            return orderBysStr;
        }
    }

    public List<OrderBy> getOrderByList() {
        return orderBys;
    }

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain() {
        PageDomain pageDomain = new PageDomain();
        if (ObjectUtil.isNotNull(ServletUtils.getAttribute(PAGE_NUM)) || ObjectUtil.isNotNull(ServletUtils.getAttribute(PAGE_SIZE))) {
            if (ObjectUtil.isNotNull(ServletUtils.getAttribute(PAGE_NUM))) {
                pageDomain.setPageNum(Convert.toInt(ServletUtils.getAttribute(PAGE_NUM)));
            }
            if (ObjectUtil.isNotNull(ServletUtils.getAttribute(PAGE_SIZE))) {
                pageDomain.setPageSize(Convert.toInt(ServletUtils.getAttribute(PAGE_SIZE)));
            }
        } else {
            pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1));
            pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10));
        }
        String parameter = ServletUtils.getParameter(ORDER_BY_COLUMN);
        if (StringUtils.isNotEmpty(parameter)) {
            String[] strings = parameter.split(",");
            List<OrderBy> orderByList = new ArrayList<>();
            for (String string : strings) {
                OrderBy orderBy = new OrderBy();
                orderBy.setOrderByColumn(string.substring(0, string.indexOf("-")));
                orderBy.setIsAsc(string.substring(string.indexOf("-") + 1));
                orderByList.add(orderBy);
            }
            pageDomain.setOrderBys(orderByList);
        }
        pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE));
        return pageDomain;
    }

    @Data
    public static class OrderBy {

        /**
         * 排序列
         */
        private String orderByColumn;

        /**
         * 排序的方向desc或者asc
         */
        private String isAsc = "asc";

        public String getOrderBy() {
            if (StringUtils.isEmpty(orderByColumn)) {
                return "";
            }
            return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
        }

        public void setIsAsc(String isAsc) {
            if (StringUtils.isNotEmpty(isAsc)) {
                // 兼容前端排序类型
                if ("ascending".equals(isAsc)) {
                    isAsc = "asc";
                } else if ("descending".equals(isAsc)) {
                    isAsc = "desc";
                }
                this.isAsc = isAsc;
            }
        }

    }

}